package com.probiz.estore.customer.service.impl;

import java.util.List;

import com.probiz.estore.common.model.customer.Consult;
import com.probiz.estore.core.search.SearchCriteria;
import com.probiz.estore.core.service.impl.GenericManagerImpl;
import com.probiz.estore.customer.dao.ConsultDao;
import com.probiz.estore.customer.model.ConsultSearchCriteria;
import com.probiz.estore.customer.service.ConsultManager;


/**
 * Manager implementation for Consult, responsible for business processing, and communicate between web and persistence layer.
 */
public class ConsultManagerImpl extends GenericManagerImpl<Consult> implements ConsultManager {

	private ConsultDao consultDao = null;

	/**
	 * @param consultDao
	 *            the consultDao to set
	 */
	public void setConsultDao(ConsultDao consultDao) {
		this.consultDao = consultDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#init()
	 */
	@Override
	protected void initManager() {
		dao = consultDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#onDelete(java.lang.Object)
	 */
	@Override
	protected void preDelete(Consult entity) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#onSave(java.lang.Object)
	 */
	@Override
	protected void preSave(Consult entity) {

	}
	
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.service.ConsultManager#deleteFully(com.probiz.estore.common.model.customer.Consult)
	 */
	public void deleteFully(Consult consult){
		//先删除咨询再统计
		this.delete(consult);
		if(consult.getParent() == null){
			//case:评论
		}else{
			//case:回复
			if(consult.getStatus().equals(Consult.STATUS_SHOW)){
				updateConsultReplyCount(consult.getParent(),-1,-1);
			}else{
				updateConsultReplyCount(consult.getParent(),-1,0);
			}
		}
	}
	
	 /*
	  * (non-Javadoc)
	  * @see com.probiz.estore.customer.service.ConsultManager#addReply(com.probiz.estore.common.model.customer.Consult)
	  */
	public void addReply(Consult reply){
		if(reply.getConsultId() == null){
			//默认情况： 新增回复必定可见
			updateConsultReplyCount(reply.getParent(),1,1);
		}
		consultDao.save(reply);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.service.ConsultManager#addConsult(com.probiz.estore.common.model.customer.Consult)
	 */
	public void addConsult(Consult consult){
		consultDao.save(consult);
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.service.ConsultManager#searchByCriteria(com.probiz.estore.customer.model.ConsultSearchCriteria, com.probiz.estore.core.search.SearchCriteria)
	 */
	public List<Consult> searchByCriteria(ConsultSearchCriteria consultSearchCriteria, SearchCriteria searchCriteria){
		return consultDao.searchByCriteria(consultSearchCriteria, searchCriteria);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.service.ConsultManager#searchByCriteriaForController(com.probiz.estore.customer.model.ConsultSearchCriteria, com.probiz.estore.core.search.SearchCriteria)
	 */
    public List<Consult> searchByCriteriaForController(ConsultSearchCriteria consultSearchCriteria, SearchCriteria searchCriteria){
    	if (searchCriteria.getPageNo() < 1)  //如果是翻页过前，就拿第一页就可以。
        {
            searchCriteria.changePaging(1, searchCriteria.getPageSize());
        }
	    List<Consult> result = searchByCriteria(consultSearchCriteria, searchCriteria);
	    if (result == null|| result.size() == 0 )
	    {
	        if (searchCriteria.getPageNo() > searchCriteria.getTotalPageCount()) //如果是翻页前，就拿最后一页就可以。
	        {
	            searchCriteria.changePaging(searchCriteria.getTotalPageCount(), searchCriteria.getPageSize());
	            result = searchByCriteria(consultSearchCriteria, searchCriteria);
	        }
	    }
		return result;
    }
    /*
     * (non-Javadoc)
     * @see com.probiz.estore.customer.service.ConsultManager#doMarkRead(com.probiz.estore.common.model.customer.Consult)
     */
    public void doMarkRead(Consult consult){
    	consult.setIsRead(Consult.STATUS_READ);
		save(consult);
    }
    
    /*
     * (non-Javadoc)
     * @see com.probiz.estore.customer.service.ConsultManager#doMarkUnRead(com.probiz.estore.common.model.customer.Consult)
     */
    public void doMarkUnRead(Consult consult){
    	consult.setIsRead(Consult.STATUS_UNREAD);
		save(consult);
    }
    
    /*
     * (non-Javadoc)
     * @see com.probiz.estore.customer.service.ConsultManager#doMarkShow(com.probiz.estore.common.model.customer.Consult)
     */
    public void doMarkShow(Consult consult){
    	Short preStatus = consult.getStatus().shortValue();
    	consult.setStatus(Consult.STATUS_SHOW);
    	save(consult);
    	if(consult.getParent() == null){
			//case:评论
		}else{
			//case:回复
			if(!preStatus.equals(Consult.STATUS_SHOW)){
				updateConsultReplyCount(consult.getParent(),0,1);
			}
		}
    }
    
    /*
     * (non-Javadoc)
     * @see com.probiz.estore.customer.service.ConsultManager#doMarkUnShow(com.probiz.estore.common.model.customer.Consult)
     */
    public void doMarkUnShow(Consult consult){
    	Short preStatus = consult.getStatus().shortValue();
    	consult.setStatus(Consult.STATUS_UNSHOW);
    	save(consult);
    	if(consult.getParent() == null){
		}else{
			//case:回复
			if(!preStatus.equals(Consult.STATUS_UNSHOW)){
				updateConsultReplyCount(consult.getParent(),0,-1);
			}
		}
    }
    
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.service.ConsultManager#updateConsultReplyCount(com.probiz.estore.common.model.customer.Consult, java.lang.Integer, java.lang.Integer)
	 */
	public void updateConsultReplyCount(Consult consult,Integer replyCount, Integer onShowReplyCount){
		if(consult == null)return;
		consult.setReplyCount(consult.getReplyCount().intValue()+replyCount.intValue());
		consult.setOnShowReplyCount(consult.getOnShowReplyCount().intValue()+onShowReplyCount.intValue());
		this.save(consult);
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.service.ConsultManager#getOnShowConsultCountByProductId(java.lang.Integer)
	 */
	public Long getOnShowConsultCountByProductId(Integer productId){
		return consultDao.getOnShowConsultCountByProductId(productId);
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.service.ConsultManager#getOnShowReplyListByConsultId(java.lang.Integer)
	 */
	public List<Consult> getOnShowReplyListByConsultId(Integer consultId){
		return consultDao.getOnShowReplyListByConsultId(consultId);
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.service.ConsultManager#getCountsOfCustomer(java.lang.Integer)
	 */
	public Integer[] getCountsOfCustomer(Integer customerId){
		return consultDao.getCountsOfCustomer(customerId);
	}
	

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.service.ConsultManager#countConsultTotal()
	 */
	public Long countConsultTotal(){
		return consultDao.countConsultTotal();
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.service.ConsultManager#countUnreplyConsultTotal()
	 */
	public Long countUnreplyConsultTotal(){
		return consultDao.countUnreplyConsultTotal();
	}
}
